package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.MchntAcctSumMapper;
import cc.rengu.igas.bsps.common.dao.impl.MchntAcctSumMapperImpl;
import cc.rengu.igas.bsps.common.entity.BspsMchntAcctSum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntAcctSumBean;
import cc.rengu.igas.bsps.facade.request.MchntSettlementFailQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntSettlementFailQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户待结算失败余额查询.
 *
 * @author zhangxuran02 (联调修改)
 * @version 1.0
 * @date 2021.01.14
 */
public class MchntSettlementFailQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntSettlementFailQueryRequest mchntSettlementFailQueryRequest = new MchntSettlementFailQueryRequest();
        ConvertUtil.convertOutput(mchntSettlementFailQueryRequest);
        return mchntSettlementFailQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntSettlementFailQueryRequest mchntSettlementFailQueryRequest = (MchntSettlementFailQueryRequest) request;
        String mchntNo = mchntSettlementFailQueryRequest.getMchntNo();
        String startDate = mchntSettlementFailQueryRequest.getStartDate();
        String endDate = mchntSettlementFailQueryRequest.getEndDate();
        int pageIndex = mchntSettlementFailQueryRequest.getPageIndex();
        int pageSize = mchntSettlementFailQueryRequest.getPageSize();
        if (StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<MchntSettlementFailQueryResponse> bizResponse = new BizResponse<>();
        MchntSettlementFailQueryResponse mchntAcctBalanceQueryResponse = new MchntSettlementFailQueryResponse();

        MchntAcctSumMapper mchntAcctSumMapper = new MchntAcctSumMapperImpl();
        /**查询失败的结算列表*/
        PageResult pageResult = mchntAcctSumMapper.selectMchntSettlementFailQuery(mchntNo, startDate, endDate, pageInfo);
        List<MchntAcctSumBean> list = new ArrayList<>();
        if (pageResult == null || pageResult.getResult().isEmpty()) {
            rglog.info("根据查询条件，数据为0条!");
            mchntAcctBalanceQueryResponse.setTotalPage(0);
            mchntAcctBalanceQueryResponse.setTotalRows(0);
        } else {

            for (Object object : pageResult.getResult()) {
                BspsMchntAcctSum bspsMchntAcctSum = (BspsMchntAcctSum) object;
                MchntAcctSumBean mchntAcctSumBean = new MchntAcctSumBean();
                BeanUtil.beanCopy(bspsMchntAcctSum, mchntAcctSumBean);
                list.add(mchntAcctSumBean);
            }
            mchntAcctBalanceQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            mchntAcctBalanceQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }

        BspsMchntAcctSum mchntAcctSum = mchntAcctSumMapper.selectMchntSettlementFailSum(mchntNo, startDate, endDate);
        String mchntRealNetAmt = mchntAcctSum.getMchntRealNetAmt();

        mchntAcctBalanceQueryResponse.setMchntRealNetAmt(mchntRealNetAmt);
        mchntAcctBalanceQueryResponse.setMchntAcctSumBeanList(list);
        /* 组装应答报文 */
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntAcctBalanceQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntSettlementFailQueryResponse mchntSettlementFailQueryResponse = (MchntSettlementFailQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntSettlementFailQueryResponse);
    }
}
